package com.zhanglifan.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zhanglifan.domain.Dept;
import com.zhanglifan.domain.Role;
import com.zhanglifan.domain.User;
import com.zhanglifan.service.DeptService;
import com.zhanglifan.service.RoleService;
import com.zhanglifan.service.UserService;
import com.zhanglifan.util.Page;
import com.zhanglifan.util.SysConstant;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.*;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2017/9/23 22:08
 * Description: UserAction
 */
public class UserAction extends BaseAction implements ModelDriven<User>{


    private Page<User> page = new Page<>();
    private User user = new User();
    private UserService userService;
    private DeptService deptService;
    private RoleService roleService;
    private String[] roleIds;
    private SimpleMailMessage mailMessage;
    private JavaMailSenderImpl mailSender;

    public void setMailMessage(SimpleMailMessage mailMessage) {
        this.mailMessage = mailMessage;
    }

    public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }

    public String list(){
        page = userService.findPage("from User", this.page, User.class, null);
        page.setUrl("userAction_list");
        super.push(page);
        return "listSuccess";
    }

    public String toview(){
        User u = (User) userService.get(User.class, user.getId());
        super.push(u);
        return "viewSuccess";
    }


    public String tocreate(){
        List<User> userList =  userService.find("from User where state=1", User.class, null);
        List<Dept> deptList = deptService.find("from Dept where state=1",Dept.class,null);
        super.put("userList",userList);
        super.put("deptList",deptList);

        return "toCreateSuccess";
    }

    public String insert(){
        String uuid = UUID.randomUUID().toString();
        user.setId(uuid);
        user.getUserinfo().setId(uuid);
        userService.saveOrUpdate(user);

        try {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mailMessage.setSubject("测试邮件标题");
                    mailMessage.setText("用户名是:"+user.getUserName()+",密码是:"+ SysConstant.DEFAULT_PASS);
                    mailMessage.setTo(user.getUserinfo().getEmail());
                    mailSender.send(mailMessage);
                }
            }).start();
        }catch (Exception e){
            e.printStackTrace();
        }

        return "insertSuccess";
    }

    public String toupdate(){
        List<Dept> deptList = deptService.find("from Dept where state=1", Dept.class, null);
        super.put("deptList",deptList);
        User u = (User) userService.get(User.class, user.getId());
        super.push(u);
        return "toUpdateSuccess";
    }

    public String update(){
        User u = (User) userService.get(User.class,user.getId());
        u.setDept(user.getDept());
        u.setState(user.getState());
        u.setUserName(user.getUserName());
        userService.saveOrUpdate(u);
        return "updateSuccess";
    }


    public String delete(){
        userService.deleteById(User.class,user.getId());
        return "deleteSuccess";
    }

    public String torole(){
        User u = (User) userService.get(User.class, user.getId());
        List<Role> roleList = roleService.find("from Role", Role.class, null);
        StringBuilder sb = new StringBuilder();

        Set<Role> roles = u.getRoles();
        for (Role role  : roles) {
            sb.append(role.getName()).append(",");
        }

        super.push(u);
        super.put("roleList",roleList);
        super.put("roleStr",sb.toString());

        return "toRoleSuccess";
    }

    public String role(){
        System.out.println("*** roleIds 的值是: " + Arrays.toString(roleIds) + " ***");
        Set<Role> roles = new HashSet<>();
        for (String id : roleIds) {
            roles.add(roleService.get(Role.class,id));
        }
        User u = (User) userService.get(User.class, user.getId());
        u.setRoles(roles);

        userService.saveOrUpdate(u);
        return "roleSuccess";
    }

    public void setRoleIds(String[] roleIds) {
        this.roleIds = roleIds;
    }

    public Page<User> getPage() {
        return page;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    public void setDeptService(DeptService deptService) {
        this.deptService = deptService;
    }

    @Override
    public User getModel() {
        return user;
    }
}
